
function calculate() {
    $.ajax({
        url:'http://49.235.114.195:8080/calculate/standard',
        data: JSON.stringify(his_in.value),
        contentType:"application/json",
        type:'POST',
        timeout:2000,
        success:function (data) {
            if(data.code!==200){
                alert('网络错误，请稍后重试');
            }else if(data.data.indexOf('错误')!==-1){
                alert('error:'+data.data);
            }else{
                clear();
                res.value=data.data;
                stack_del.push(data.data);
                his_in.value='';
                return;
            }
            clear();
        },
        error:function () {
            alert('网页发生错误，请稍后重试');
            clear();
        }
    })
}

function cal_equ(val){
    let str=get_equ();
    switch (val) {
        case '2^x':
            str='2^('+str+')';
            break;
        case 'x^2':
            str='sqr('+str+')';
            break;
        case '1/x':
            str='1/('+str+')';
            break;
        case '|x|':
            str='abs('+str+')';
            break;
        case '√x':
            str='√('+str+')';
            break;
        case '10^x':
            str='10^('+str+')';
            break;
        case 'log':
            str='log('+str+')';
            break;
        case 'ln':
            str='ln('+str+')';
            break;
        case '+/-':
            str='(-('+str+'))';
            break;
        case 'n!':
            str='('+str+')!';
            break;
        default:
            alert('错误:switch');
            break;
    }
    his_in.value=his_in.value+str;
    stack_equ.push(str);
}
function get_equ(){
    let ans_str='';
    let len=0,cnt=0;
    let stack_get=new Stack();
    while(!stack_equ.isEmpty() && cal_symbol_map.indexOf(stack_equ.peek().toString())!==-1 && stack_equ.peek()!==')'){
        len+=stack_equ.peek().toString().length;
        stack_get.push(stack_equ.peek());
        stack_equ.pop();
    }
    if(stack_equ.peek()===')'){
        cnt++;
        stack_equ.pop();
        len++;
        while(!stack_equ.isEmpty() && cnt>0){
            if(stack_equ.peek()===')') cnt++;
            else if(stack_equ.peek()==='(') cnt--;
            len+=stack_equ.peek().toString().length;
            if(cnt>0) stack_get.push(stack_equ.peek());
            stack_equ.pop();
        }
    }else if(cal_symbol_map.indexOf(stack_equ.peek().toString())===-1){
        len+=stack_equ.peek().toString().length;
        stack_get.push(stack_equ.peek());
        stack_equ.pop();
    }else if(stack_equ.isEmpty()){
        alert('错误:get_equ');
    }
    delete_(len);
    while(!stack_get.isEmpty()){
        ans_str+=stack_get.peek().toString();
        stack_get.pop();
    }
    return ans_str;
}
function delete_(val){
    if(his_in.value.length - val <= 0){
        his_in.value='';
    }else{
        his_in.value=his_in.value.substring(0,his_in.value.length-val);
    }
}